//
//  SEGLuNengHotLineViewController.m
//  Commom
//
//  Created by 梁学彰 on 2020/3/16.
//

#import "SEGLuNengHotLineViewController.h"
#import "SEGPlatformHeader.h"
#import "CommomHeader.h"
#import "SEGHotLineCardTableViewCell.h"
#import "SEGHotLineManagerCollectionViewTableViewCell.h"
#import "SEGStandard-prefix.pch"

//#import <SEGViewModel/SEGServiceHotLineViewModel.h>

#import "SEGServiceHotLineViewModel.h"
#import "ReactiveObjC.h"


@interface SEGLuNengHotLineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) SEGServiceHotLineViewModel *viewModel;

@end

@implementation SEGLuNengHotLineViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubViews];
    [self bindViewModel];
}

#pragma mark - init
- (void)initSubViews {
    [self.view addSubview:self.table];
}

- (void)bindViewModel {
    self.viewModel = [SEGServiceHotLineViewModel new];
    WeakSelf;
    //订阅组合信号
    [SVProgressHUD show];
    [self.viewModel loadData];
    [self.viewModel.concatSignal subscribeNext:^(id  _Nullable x) {
        [SVProgressHUD dismiss];
        [weakSelf reloadTableViewAction];
    }];
}


#pragma mark - UITableViewDelegate, UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return LYScreenWidth() - 80 + 30 + 104 + 10;
    }
    return 87;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 25;
    }
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return 30;
    }
    return 0.01;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        if (self.viewModel.manager.dataSourceArr.count >= 1) {
            return 1;
        }
    }
    if (section == 1) {
        if (self.viewModel.telString.length > 0) {
            return 1;
        }
    }
    if (section == 2) {
        if (self.viewModel.phoneString.length > 0) {
            return 1;
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SEGHotLineManagerCollectionViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SEGHotLineManagerCollectionViewTableViewCell" forIndexPath:indexPath];
        if (self.viewModel.manager.dataSourceArr) {
            cell.dataArray = self.viewModel.manager.dataSourceArr;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    SEGHotLineCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SEGHotLineCardTableViewCell" forIndexPath:indexPath];
    if (indexPath.section == 1) {
        cell.titleLabel.text = @"物业客服中心";
        cell.telLabel.text = self.viewModel.telString;
    }else if (indexPath.section == 2) {
        cell.titleLabel.text = @"全国监督服务热线";
        cell.telLabel.text = self.viewModel.phoneString;
    }
    return cell;
}

#pragma mark - private methods
- (void)reloadTableViewAction {
    if (self.viewModel.phoneString.length == 0&&
        self.viewModel.telString.length == 0&&
        self.viewModel.manager.dataSourceArr.count == 0) {
        [[SEGUhomeMediator sharedInstance] seg_showCustomProgressImage:@{@"view":self.view}];
    }else {
        [[SEGUhomeMediator sharedInstance] seg_hideCustomProgressImage:@{@"view":self.view}];
    }
    [self.table reloadData];
}
#pragma mark - setter and getter
- (UITableView *)table {
    if (!_table) {
        _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight() - kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_table registerClass:[SEGHotLineCardTableViewCell class] forCellReuseIdentifier:@"SEGHotLineCardTableViewCell"];
        [_table registerClass:[SEGHotLineManagerCollectionViewTableViewCell class] forCellReuseIdentifier:@"SEGHotLineManagerCollectionViewTableViewCell"];
        if (@available(iOS 11.0, *)) {
            _table.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _table;
}

@end
